package demo1;

/**
 * @Author liangzai
 * @Description:
 */
public class Training {
    //交替组 1
    public int numberOfAlternatingGroups(int[] colors) {
        int ans = 0;
        int n = colors.length;
        for (int i = 0; i < n; i++) {
            if (colors[(n + i - 1) % n] != colors[i] && colors[i] != colors[(i + 1) % n]) {
                ans++;
            }
        }
        return ans;
    }

    //生命游戏
    int[][] dirs = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};
    public void gameOfLife(int[][] board) {
        int m = board.length, n = board[0].length;
        int[][] res = new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                int cnt = 0;
                for (int[] d : dirs) {
                    int x = i + d[0], y = j + d[1];
                    cnt += x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 1 ? 1 : 0;
                }
                res[i][j] = (board[i][j] == 0 && cnt == 3) || (board[i][j] == 1 && (cnt == 2 || cnt == 3)) ? 1 : 0;
            }
        }
        for (int i = 0; i < m; i++) System.arraycopy(res[i], 0, board[i], 0, n);
    }
}
